#include "radio_config.h"
#define USART_RX_BUFFER_SIZE 254
#define USART_TX_BUFFER_SIZE 254
#define USART_DATA_SIZE 128
#define Inc_RX_Ptr(Addr)	{ Addr++;	if(Addr >= USART_RX_BUFFER_SIZE) { Addr=0; } }
#define ClearRxBuffer()     { RxHead=RxTail; } 
#define RxBuffDataLen	    ( RxTail+USART_RX_BUFFER_SIZE -RxHead )%USART_RX_BUFFER_SIZE

#define Inc_TX_Ptr(Addr)	{ Addr++;	if(Addr >= USART_TX_BUFFER_SIZE) { Addr=0; } }
#define ClearTxBuffer()     { TxHead=TxTail; } 
#define TxBuffDataLen	    ( TxTail+USART_TX_BUFFER_SIZE -TxHead )%USART_TX_BUFFER_SIZE
unsigned char  RxBuffer[USART_RX_BUFFER_SIZE]; 
unsigned char  TxBuffer[USART_TX_BUFFER_SIZE]; 
unsigned char  RxHead, RxTail;
unsigned char  TxHead, TxTail;
unsigned char UartData[USART_DATA_SIZE];
unsigned char UartDataLen;
void serialize16(u16 a) {SerialUSB.write( a); SerialUSB.write( (a>>8)&0xff);}
void serialize8(u8 a)  {SerialUSB.write( a);}
void UartReceive(unsigned char rxChar)
{ 
   unsigned char tmpTail;
	 //---------------------modbus receiver-----------------------------------------
            RxBuffer[RxTail]=rxChar;  
            tmpTail=RxTail;
           Inc_RX_Ptr(tmpTail);//
           if(tmpTail!=RxHead)
            {  
               RxTail=tmpTail;
             }
	m_toSendTimeOut_ct=1;
	 //------------------------------------------------------------------------------
   

}

unsigned char  GetUartByte(u8 *DataPtr)
{// read a byte data from the comm receiver buff
	if( RxTail != RxHead )
	{	   
	   *DataPtr = RxBuffer[RxHead];		 
	   Inc_RX_Ptr(RxHead);	// adjust the head 
	   return(TRUE);
	}
	else
	{
	  return(FALSE);
	}
} 
unsigned char GetUartData(u8   *DataPtr)
{    
    unsigned char  len;
    len=0;
	while(GetUartByte(DataPtr)==TRUE)
	{    
	    DataPtr++;
	    len++;
	    if(len>=MAX_FIFO_FIELD2_LEN)break;//限制最大长度  
	}
	return(len);	
}
u8 GetSendByte(u8 * dataPtr)
{
      if( TxTail != TxHead )
	{	   
	   *dataPtr = TxBuffer[TxHead];		 
	   Inc_TX_Ptr(TxHead);	// adjust the head 
	   return(TRUE);
	}
	else
	{
	  return(FALSE);
	} 
}
u8 GetSendData(u8   *DataPtr)
{
   unsigned char  len=0;
   len=0;
	while(GetSendByte(DataPtr)==TRUE)
	{    
	    DataPtr++;
	    len++;
	     
	}
	return(len);	
   
}
void PutUartSendByte(u8 data)
{
   unsigned char tmpTail;
	 //---------------------modbus receiver-----------------------------------------
            TxBuffer[TxTail]=data;  
            tmpTail=TxTail;
           Inc_TX_Ptr(tmpTail);//
           if(tmpTail!=TxHead)
            {  
               TxTail=tmpTail;
             }
}
void PutUartSendData(u8 *DataPtr,u8 len)
{u8 i;
  for(i=0;i<len;i++)
  {
    PutUartSendByte(DataPtr[i]);
  }
}
void SendData(u8 *DataPtr,u8 len)
{  u8 i;
  for(i=0;i<len;i++)
  {serialize8(DataPtr[i]);
    //SerialUSB.print(DataPtr[i],HEX);
  }
  SerialUSB.print("\r\n");
}


 
void LoopSerialCom() 
{  
  if (  SerialUSB.available()) 
  {
   UartReceive(SerialUSB.read( ));
  }
  
}


void LoopCheckDataRFSend(void)
{
   u8 RxLen;
   if((m_toSendTimeOut_ct>80)||(RxBuffDataLen>=MAX_FIFO_FIELD2_LEN))//接收超时或者接收缓冲区数据超过32个数据就发送
     {
          
        if ((radio_hal_NirqLevel() == HIGH)&&(m_RFTxTimeOut_ct>=RF_SEND_MAX_TIME))//空闲
        {
          
          RxLen=GetUartData(&UartData[1]);
          
          if(RxLen)
          {
            
           if(RxLen>=MAX_FIFO_FIELD2_LEN)m_toSendTimeOut_ct=1;//表示还有数据
           else m_toSendTimeOut_ct=0; 
           
           RxLen+=1;
         
           UartData[0]=RxLen;
            //SerialUSB.print("SendData:\r\n");SerialUSB.print(RxLen);
            radio.vRadio_StartTx_Variable_Packet(pRadioConfiguration->Radio_ChannelNumber,UartData,RxLen);          
           m_RFTxTimeOut_ct=0;
         
        }
      }
     }
  
}
void LoopSi446xFun(void)
{
	
	  radioStat = radio.bRadio_Check_Tx_RX();
  
   
   switch (radioStat)
     {
        case SI446X_CMD_GET_INT_STATUS_REP_PACKET_SENT_PEND_BIT:  
	       
              
              radio.si446x_set_property(0x12,3,0x08,0x0a,0x01,0x00); //set PKT_LEN,PKT_LEN_FIELD_SOURCE ,PKT_LEN_FIELD_SOURCE 
              break;

	case SI446X_CMD_GET_INT_STATUS_REP_PACKET_RX_PEND_BIT:     //接收到数据      
            
            if((variableRadioPacket[0]==4)&&(variableRadioPacket[1]=='A')&&(variableRadioPacket[2]=='C')&&(variableRadioPacket[3]=='K'))
            {//收到应答信号
                m_RFTxTimeOut_ct=RF_SEND_MAX_TIME;//发送完成 直接值为发送超时计时来表示发送完成
           
            }
            else 
            {
              
              
               if(variableRadioPacket[0]>=Si446xCmd.FIFO_INFO.RX_FIFO_COUNT)
               variableRadioPacket[0]=Si446xCmd.FIFO_INFO.RX_FIFO_COUNT;
               if(variableRadioPacket[0]<=64)
               {//接收到有效数据
                
               // SendData(&variableRadioPacket[1],variableRadioPacket[0]-1);  //串口发送出去                        
                if(variableRadioPacket[0]==17)
                {
                	for(int i=0;i<8;i++)
                    {
                      pwmVal[i]=variableRadioPacket[1+i*2];
                      pwmVal[i]=(pwmVal[i]<<8)+ variableRadioPacket[1+i*2+1];
                      pwmWrite(PWM_PIN[i],pwmVal[i]);
                   }
                }
               }
                else
                {// 表示接收数据长度错误
                  SerialUSB.print(0X55);
                  SerialUSB.print(0XEE);
                  SerialUSB.print(0XAA);
                 }
              ;
#ifndef  DISABLE_ACK
              radio.vRadio_StartTx_Variable_Packet(pRadioConfiguration->Radio_ChannelNumber,(u8 *)m_RFAck,sizeof(m_RFAck));//返回应答 
#endif
            }
              
            
          
            
	   break;
	default:
	       break;
     } 
}